package com.seckill.controller;


import com.seckill.domain.Product;
import com.seckill.domain.ProductSku;
import com.seckill.service.ProductService;
import com.seckill.service.ProductSkuService;

import com.seckill.utils.R;
import com.seckill.vo.ProductVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.HashMap;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("/product")
public class ProductController {
    @Autowired
    private ProductService productService;
    @Autowired
    private ProductSkuService productSkuService;

    /**
     * 分页获取商品列表
     */
    @GetMapping("/list")
    public R list(@RequestBody ProductVo productVo) {
        return productService.getProductPage(productVo);
    }


    /**
     * 获取商品列表（秒杀页面展示用）
     */
    @GetMapping("/seckill-list")
    public R seckillList() {
        return R.OK(productService.getAllProducts());
    }

    @PostMapping("/uplodFile")
    public R uplodFile(@RequestPart("file") MultipartFile file){
        return productService.uplodFile(file);
    }

    /**
     * 查询商品详情及规格
     */
    @GetMapping("/detail/{id}")
    public R getProductDetail(@PathVariable("id") Long id) {
        Product product = productService.getProductById(id);
        List<ProductSku> skuList = productSkuService.getSkuListByProductId(id);
        Map<String, Object> result = new HashMap<>();
        result.put("product", product);
        result.put("skuList", skuList);
        return R.OK(result);
    }
} 